////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.logging.formatters {

import ru.goodcore.logging.ILogger;
import ru.goodcore.logging.LogEvent;
import ru.goodcore.utils.StringUtils;

//import ru.goodcore.logging.ILogger;

/**
 *
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					loggingtarget, debugtarget, target, builtin
 */
public class LineFormatter implements ITargetMessageFormatter {

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function LineFormatter() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//  Protected variables
	//
	//-------------------------------------------------------------------------

	protected var lastMeasuredTime:	Number = new Date().getTime();

	//-------------------------------------------------------------------------
	//
	//  Properties. Interface implementation: ITargetMessageFormatter
	//
	//-------------------------------------------------------------------------

	//----------------------------------
	//  fieldSeparator
	//----------------------------------
	/**
	 * @private
	 */
	private var _fieldSeparator:	String = ' ';

	[Inspectable(category="General", defaultValue=" ")]

	/**
	 *  The separator string to use between fields (the default is " ")
	 */
	public function set fieldSeparator(value:String):void {
		this._fieldSeparator = value;
	}

	public function get fieldSeparator():String {
		return this._fieldSeparator;
	}


	//----------------------------------
	//  includeCategory
	//----------------------------------
	/**
	 * @private
	 */
	private var _includeCategory:	Boolean = true;

	[Inspectable(category="General", defaultValue="true")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public function set includeCategory(value:Boolean):void {
		this._includeCategory = value;
	}

	public function get includeCategory():Boolean {
		return this._includeCategory;
	}

	//----------------------------------
	//  includeDate
	//----------------------------------
	/**
	 * @private
	 */
	private var _includeDate:	Boolean;

	[Inspectable(category="General", defaultValue="false")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public function set includeDate(value:Boolean):void {
		this._includeDate = value;
	}

	public function get includeDate():Boolean {
		return this._includeDate;
	}

	//----------------------------------
	//  includeLevel
	//----------------------------------
	/**
	 * @private
	 */
	private var _includeLevel:	Boolean = true;

	[Inspectable(category="General", defaultValue="true")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public function set includeLevel(value:Boolean):void {
		this._includeLevel = value;
	}

	public function get includeLevel():Boolean {
		return this._includeLevel;
	}

	//----------------------------------
	//  includeTime
	//----------------------------------
	/**
	 * @private
	 */
	private var _includeTime:	Boolean = true;

	[Inspectable(category="General", defaultValue="true")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public function set includeTime(value:Boolean):void {
		this._includeTime = value;
	}

	public function get includeTime():Boolean {
		return this._includeTime;
	}

	//----------------------------------
	//  includeMeasuredTime
	//----------------------------------
	/**
	 * @private
	 */
	private var _includeMeasuredTime:Boolean = true;

	[Inspectable(category="General", defaultValue="fasle")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public function set includeMeasuredTime(value:Boolean):void {
		this._includeMeasuredTime = value;
	}

	public function get includeMeasuredTime():Boolean {
		return this._includeMeasuredTime;
	}

	//-------------------------------------------------------------------------
	//
	//  Public methods. Interface implementation: ITargetMessageFormatter
	//
	//-------------------------------------------------------------------------
	
	public function formatMessage(event:LogEvent):String {
		var message:String = '';

		if (
			this.includeDate || 
			this.includeTime || 
			this.includeMeasuredTime			
		) {

			var date:String = '';

			var d:Date = new Date();
			

			if (this.includeDate) {
				date = Number(d.getMonth() + 1).toString() + '/' +
						d.getDate().toString() + '/' + 
						d.getFullYear() + this.fieldSeparator;
			}

			if (this.includeTime) {
				date += this.padTime(d.getHours()) + ':' +
						this.padTime(d.getMinutes()) + ':' +
						this.padTime(d.getSeconds()) + '.' +
						this.padTime(d.getMilliseconds(), true) + this.fieldSeparator;
			}

			if (this.includeMeasuredTime) {
				const currentTime:	Number = d.getTime();
				date += '+' + StringUtils.setNumberLen(currentTime - this.lastMeasuredTime, 5) + 
						this.fieldSeparator;
				this.lastMeasuredTime = currentTime;
			}

			message += date;
		}

		if (this.includeLevel) {
			var level:	String = '[' + 
					LogEvent.getLevelString(event.level) +
					']' + this.fieldSeparator;

			message += level;
		}

		var category:String = this.includeCategory ?
			ILogger(event.target).category + this.fieldSeparator : '';

		message +=  category ;

		message += event.message;

		return message;
	}

	//-------------------------------------------------------------------------
	//
	//  Protected methods
	//
	//-------------------------------------------------------------------------

	/**
	 *  @private
	 */
	protected function padTime(num:Number, millis:Boolean = false):String {
		if (millis) {
			if (num < 10)
				return '00' + num.toString();
			else if (num < 100)
				return '0' + num.toString();
			else 
				return num.toString();
		}
		else {
			return num > 9 ? num.toString() : '0' + num.toString();
		}
	}


}
}